package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.os.Bundle;

import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.OfficialImAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.OfficialMessage;
import com.luoxiang.socialandroid.bean.OfficialMessageBean;
import com.luoxiang.socialandroid.bean.Result;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.header.ClassicsHeader;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class OfficialImActivity extends BaseActivity {
    private OfficialImAdapter adapter;
    private final ArrayList<OfficialMessage> data = new ArrayList<>();

    private RecyclerView recyclerView;
    private RefreshLayout refreshLayout;

    private int page = 1;

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            getData();
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            getData();
        });
        findViewById(R.id.icon_back).setOnClickListener(v -> finish());
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_official_im);
    }

    @Override
    protected void initView() {
        setStatusBarTranslucent();
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);
        initRefreshLayout();
        adapter = new OfficialImAdapter(data);
        recyclerView.setAdapter(adapter);

        getData();
    }

    private void initRefreshLayout() {
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(true);
//        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setDisableContentWhenRefresh(true);
    }

    private void getData(){
        getRequest();
        NetUtils.getInstance().getApiService().officialMessage(MmkvUtil.INSTANCE.decodeString(TOKEN), page,100)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Result result = new Gson().fromJson(decryptStr, Result.class);
                        if(result == null){
                            return;
                        }
                        getDataSuccess(result.getData().getMessages());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getDataSuccess(ArrayList<OfficialMessage> officialMessageArrayList){
        //因为后端数据没有分页信息，不做加载更多的逻辑
        if(page == 1){
            refreshLayout.finishRefresh();
            data.clear();
        }else {
            refreshLayout.finishLoadMore();
        }
        if(officialMessageArrayList == null || officialMessageArrayList.isEmpty()){
            return;
        }
        data.addAll(officialMessageArrayList);
        adapter.notifyDataSetChanged();
    }
}
